package com.net.oil.main.fragment;


import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.app.UserInfoManager;
import com.net.oil.base.BaseFragment;
import com.net.oil.base.EventBussMessage;
import com.net.oil.car.MyCarDetailActivity;
import com.net.oil.coupon.CouponActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.luckPan.LuckPanActivity;
import com.net.oil.main.FeedbackActivity;
import com.net.oil.message.MessageActivity;
import com.net.oil.order.ReserveActivity;
import com.net.oil.user.LoginActivity;
import com.net.oil.user.MyReceiveGoodsActivity;
import com.net.oil.user.MyRecommendListActivity;
import com.net.oil.user.SetActivity;
import com.net.oil.user.UserDetailActivity;
import com.net.oil.user.bean.UserInfo;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;
import com.net.oil.util.share.ShareDialogFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 个人中心fragment
 */
public class MyFragment extends BaseFragment {

    @BindView(R.id.user_head_portrait)
    ImageView userHeadPortrait;
    Unbinder unbinder;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_sign)
    TextView userSign;
    @BindView(R.id.message_layout)
    RelativeLayout messageLayout;
    @BindView(R.id.my_car_layout)
    RelativeLayout myCarLayout;
    @BindView(R.id.set_layout)
    RelativeLayout setLayout;

    private UserInfo userInfo;
    private UserInfo.UserDetailBean userDetail;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        userInfo = UserInfoManager.getInstance().getUserInfo();
        if (userInfo != null && userInfo.getUserDetail() != null) {
            userDetail = userInfo.getUserDetail();
        }
        updateView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && isAdded()) {
            initView();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.user_head_portrait, R.id.set_layout, R.id.my_car_layout, R.id.message_layout, R.id.my_reserve_layout,
            R.id.my_coupon_layout, R.id.feedback_layout, R.id.my_goods_layout, R.id.my_recommend_layout, R.id.share_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_head_portrait:
                if (UserInfoManager.getInstance().isLogin()) {
                    startActivity(new Intent(getContext(), UserDetailActivity.class));
                } else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.set_layout:
                startActivityForResult(new Intent(getContext(), SetActivity.class), Constant.SET_REQUEST_CODE);
                break;
            case R.id.my_car_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    startActivity(new Intent(getContext(), MyCarDetailActivity.class));
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.message_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    startActivity(new Intent(getContext(), MessageActivity.class));
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.my_coupon_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    startActivity(new Intent(getContext(), CouponActivity.class));
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.my_reserve_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    startActivity(new Intent(getContext(), ReserveActivity.class));
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.feedback_layout:
                startActivity(new Intent(getContext(), FeedbackActivity.class));
                break;
            case R.id.my_goods_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    startActivity(new Intent(getContext(), MyReceiveGoodsActivity.class));
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.my_recommend_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    startActivity(new Intent(getContext(), MyRecommendListActivity.class));
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
            case R.id.share_layout:
                if (UserInfoManager.getInstance().isLogin()){
                    share();
                }else {
                    startActivityForResult(new Intent(getContext(), LoginActivity.class), Constant.LOGIN_REQUEST_CODE);
                }
                break;
        }
    }

    private void share(){
        String code = UserInfoManager.getInstance().getUserInfo().getUserDetail().getInvitationCode();
        String url = AppUrl.getShareUrl(code);
        ShareDialogFragment dialogFragment = ShareDialogFragment.newInstance(url, getString(R.string.app_name));
        dialogFragment.show(getActivity().getSupportFragmentManager(), "");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            /*if (requestCode == Constant.LOGIN_REQUEST_CODE ){
                if (data != null) {
                    userInfo = (UserInfo) data.getSerializableExtra(Constant.LOGIN_RESULT_INTENT_KEY);
                    if (userInfo != null && userInfo.getUserDetail() != null) {
                        userDetail = userInfo.getUserDetail();
                    }
                }
            }*/

            if (requestCode == Constant.SET_REQUEST_CODE){
                userInfo = null;
                userDetail = null;
            }

            updateView();
        }
    }

    private void updateView() {

        userInfo = UserInfoManager.getInstance().getUserInfo();
        if (userInfo != null && userInfo.getUserDetail() != null) {
            userDetail = userInfo.getUserDetail();
        }

        if (userDetail != null) {
            if (!StringUtil.isEmpty(userDetail.getName())) {
                userName.setText(userDetail.getName());
            }else {
                userName.setText("");
            }
            if (!StringUtil.isEmpty(userDetail.getLabel())) {
                userSign.setText(userDetail.getLabel());
            }
            if (!StringUtil.isEmpty(userDetail.getPortrait())){
                GlideUtil.getInstance().loadCircleImage(getContext(), userHeadPortrait, AppUrl.getImageUrl(userDetail.getPortrait()),
                        R.drawable.ic_user_default);
            }
        }else {
            userName.setText("请登录");
            userSign.setText("");
            GlideUtil.getInstance().loadCircleImage(getContext(), userHeadPortrait, "", R.drawable.ic_user_default);
        }
    }

    /**
     * 接受更换头像成功消息
     *
     * @param message 消息
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void receiveEventBus(EventBussMessage message) {
        if (message.getCode() == Constant.UPLOAD_USER_HEAD_SUCCESS_CODE) {
            if (!StringUtil.isEmpty(userDetail.getPortrait())){
                GlideUtil.getInstance().loadCircleImage(getContext(), userHeadPortrait, AppUrl.getImageUrl(userDetail.getPortrait()),
                        R.drawable.ic_user_default);
            }
        }

        //接受登录或者退出的消息
        if (message.getCode() == Constant.LOGIN_SUCCESS_CODE || message.getCode() == Constant.LOGOUT_CODE) {
            updateView();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
